<?php


namespace App\Http\observer;


use App\Models\HomeSet;
use App\Models\Image;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class HomeSetObserver
{
    public function saved(HomeSet $homeSet)
    {
        if (file_exists(public_path()."/".$homeSet->image)){
            $data=getimagesize(public_path()."/".$homeSet->image);
            if(isset($data[0])||isset($data[1])){
                Log::error($data);
                $homeSet["w"]=$data[0];
                $homeSet["h"]=$data[1];
                HomeSet::where("id",$homeSet["id"])->update(["w"=>$homeSet->w,'h'=>$homeSet->h]);
            }
        }
    }

    public function updating(HomeSet $homeSet)
    {
        $old_homeSet=HomeSet::where("id",$homeSet["id"])->first();
        if ($old_homeSet["image"]){
            Cache::put("home_set_image",$old_homeSet["image"]);
        }
    }

    public function updated(HomeSet $homeSet)
    {
        $home_set_image=Cache::get("home_set_image");
        Cache::forget("home_set_image");
        if ($home_set_image&&$homeSet["image"]!=$home_set_image){
            Image::del_data($home_set_image);
        }
    }

    public function deleted(HomeSet $homeSet)
    {
        if ($homeSet->image){
            Image::del_data($homeSet->image);
        }
    }


}
